//
//  SEGUserHouserRequestManager.m
//  SEGUhomePropertyBaseService
//
//  Created by 揭婧雯 on 2018/8/28.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGUserHouserRequestManager.h"

@implementation SEGUserHouserRequestManager
/**
 * 请求所有用户房屋列表接口（获取当前用户房屋列表，包含用户认证的房屋，包含申请中、通过、未通过的房屋接口）
 * @author Wendy
 * @param finished 网络请求完成后的操作
 */
-(void)requestAllMyHouseList:(SEGRequestCallBack)finished
{
    
    NSDictionary *params = @{@"custId" : ValueToString([SEGProfileManager sharedManager].profile.custId)};
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_sso,@"/v1/userHouse/myHouseList",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dataDict = [result valueForKey:@"data"];
        if (!dataDict || ![dataDict isKindOfClass:[NSDictionary class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSArray *houseList = dataDict[@"houseList"];
        if (!houseList || ![houseList isKindOfClass:[NSArray class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSMutableArray *modelArray = [NSMutableArray array];
        for (NSDictionary *modelDict in houseList) {
            SEGHouseInfoModel *model = [SEGHouseInfoModel yy_modelWithDictionary:modelDict];
            //认证状态：1已申请认证、2 认证成功、4 审核拒绝 其他不显示
            SEGHouseAuditStatus status = [model.authFlag integerValue];
            if (status == SEGHouseAuditStatus_Apply ||
                status == SEGHouseAuditStatus_Pass ||
                status == SEGHouseAuditStatus_Refuse)
            {
                [modelArray addObject:model];
            }
        }
        self.houseInfoArray = modelArray;
        finished(YES, codeType, errorMessage);
    }];
}

/**
 * 根据房屋ID,查询房屋下的用户列表
 * @author Wendy
 * @param houseId 房屋ID
 * @param finished 网络请求完成后的操作
 */
-(void)requestUserListWithHouseId:(NSString *)houseId finished:(SEGRequestCallBack)finished
{
    NSDictionary *params = @{@"houseId" : houseId};
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_sso,@"/v1/userHouse/userListByHouseId",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dataDict = [result valueForKey:@"data"];
        if (!dataDict || ![dataDict isKindOfClass:[NSDictionary class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSArray *userList = dataDict[@"userList"];
        if (!userList || ![userList isKindOfClass:[NSArray class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSMutableArray *modelArray = [NSMutableArray array];
        for (NSDictionary *modelDict in userList) {
            SEGUserInfoModel *model = [SEGUserInfoModel yy_modelWithDictionary:modelDict];
            [modelArray addObject:model];
        }
        self.userInfoArray = modelArray;
        finished(YES, codeType, errorMessage);
    }];
}


/**
 * 根据房屋ID,设置默认房屋
 * @author Wendy
 * @param houseId 房屋ID
 * @param relId 房屋关系主键ID
 * @param finished 网络请求完成后的操作
 */
-(void)requestSetDefaultHouseWithHouseId:(NSString *)houseId relId:(NSString *)relId finished:(SEGRequestCallBack)finished
{
    NSDictionary *params = @{@"houseId" : houseId,
                             @"relId" : relId
                             };
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_sso,@"/v1/userHouse/setDefaultHouse",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dataDict = [result valueForKey:@"data"];
        if (!dataDict || ![dataDict isKindOfClass:[NSDictionary class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        //设置成功的关系ID
//        NSString *relId = dataDict[@"relId"];
//        if (!relId || ![relId isKindOfClass:[NSString class]]) {
//            finished(NO, codeType, errorMessage);
//            return;
//        }
        
        finished(YES, codeType, errorMessage);
    }];
}

/**
 * 根据房屋ID和用户ID,删除房屋下的用户
 * @author Wendy
 * @param houseId 房屋ID
 * @param userId 要删除用户的ID
 * @param finished 网络请求完成后的操作
 */
-(void)requestDelHouseUserWithHouseId:(NSString *)houseId userId:(NSString *)userId finished:(SEGRequestCallBack)finished
{
    NSDictionary *params = @{@"houseId" : houseId,
                             @"userId" : userId
                             };
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_sso,@"/v1/userHouse/delHouseUserByUserId",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dataDict = [result valueForKey:@"data"];
        if (!dataDict || ![dataDict isKindOfClass:[NSDictionary class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        finished(YES, codeType, errorMessage);
    }];
}

/**
 * 用户申请房屋认证接口
 * @author Wendy
 * @param name 申请人姓名
 * @param houseId 房屋ID
 * @param relId 房屋关系主键ID
 * @param custTel 预留客户手机号
 * @param custName 预留客户姓名
 * @param finished 网络请求完成后的操作
 */
-(void)requestSubmitUserAtuhWithName:(NSString *)name
                             houseId:(NSString *)houseId
                               relId:(NSString *)relId
                             custTel:(NSString *)custTel
                            custName:(NSString *)custName
                            finished:(SEGRequestCallBack)finished
{
    NSDictionary *dict = @{
                           @"name": name,
                           @"houseId" : houseId,
                           @"custTel" : custTel,
                           @"custName" : custName
                           };
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dict];
    if (relId && relId.length > 0) {
        [params setObject:@"relId" forKey:relId];
    }
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_sso,@"/v1/userHouse/submitUserAtuh",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dataDict = [result valueForKey:@"data"];
        if (!dataDict || ![dataDict isKindOfClass:[NSDictionary class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        //设置成功的关系ID
        //        NSString *relId = dataDict[@"relId"];
        //        if (!relId || ![relId isKindOfClass:[NSString class]]) {
        //            finished(NO, codeType, errorMessage);
        //            return;
        //        }
        
        finished(YES, codeType, errorMessage);
    }];
}

/**
 * 用户申请房屋人工审核接口
 * @author Wendy
 * @param houseId 房屋ID
 * @param relId 房屋关系主键ID
 * @param name 申请人真实姓名
 * @param finished 网络请求完成后的操作
 */
-(void)requestSubmitFailUserAtuhWithHouseId:(NSString *)houseId
                                      relId:(NSString *)relId
                                       name:(NSString *)name
                                   finished:(SEGRequestCallBack)finished
{
    NSDictionary *dict = @{
                           @"houseId" : houseId,
                           @"name" : name
                           };
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dict];
    if (relId && relId.length > 0) {
        [params setObject:@"relId" forKey:relId];
    }
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_sso,@"/v1/userHouse/submitFailUserAtuh",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dataDict = [result valueForKey:@"data"];
        if (!dataDict || ![dataDict isKindOfClass:[NSDictionary class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        //设置成功的关系ID
        //        NSString *relId = dataDict[@"relId"];
        //        if (!relId || ![relId isKindOfClass:[NSString class]]) {
        //            finished(NO, codeType, errorMessage);
        //            return;
        //        }
        
        finished(YES, codeType, errorMessage);
    }];
}
@end
